package com.his.modules.sysjob.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.modules.business.entity.CaSignData;
import com.his.modules.business.entity.OmrBl02;
import com.his.modules.business.service.CaSignDataService;
import com.his.modules.business.service.OmrBl02Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component("syncOmrBl02BlobToClobJob")
@Slf4j
public class SyncOmrBl02BlobToClobJob {
    @Autowired
    private OmrBl02Service omrBl02Service;
    @Autowired
    private CaSignDataService caSignDataService;

    public void syncOmrBl02BlobToClobJob() {
        log.info("门诊病历Lob转换定时任务开始执行=================");

        QueryWrapper<OmrBl02> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("blbh", "select jlxh from ca_sign_data t where qmyw is null");
        List<OmrBl02> omrBl02List = omrBl02Service.list(queryWrapper);

        if(!CollectionUtils.isEmpty(omrBl02List)) {
            log.info("门诊病历Lob转换业务数据条数：{}=================", omrBl02List.size());

            for(OmrBl02 omr : omrBl02List) {
                String blnr = new String(omr.getBlnr());
                System.out.println(blnr);
                QueryWrapper<CaSignData> caSignDataQueryWrapper = new QueryWrapper<CaSignData>();
                caSignDataQueryWrapper.eq("jlxh", omr.getBlbh());
                List<CaSignData> caSignDataList = caSignDataService.list(caSignDataQueryWrapper);
                if(!CollectionUtils.isEmpty(caSignDataList)) {
                    for(CaSignData caSignData : caSignDataList) {
                        caSignData.setQmyw(blnr);
                    }
                    caSignDataService.updateBatchById(caSignDataList);
                }
            }
        }
    }
}